home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 3 / Cream of the Crop 3.iso / utility / fsco3712.zip / FSCODE.C < prev    next >
C/C++ Source or Header  |  1993-12-25  |  4KB  |  171 lines

  1. /*
  2. ** FSCode.c (25.12.93) by Flavio Stanchina
  3. ** Loc. Montevaccino, 39
  4. ** 38040 Trento (Italy)
  5. ** 2:333/408.9@fidonet.org
  6. */
  7.  
  8. #include <exec/types.h>
  9. #include <exec/memory.h>
  10. #include <dos/dos.h>
  11. #include <dos/rdargs.h>
  12. #include <dos/stdio.h>
  13.  
  14. #include <clib/exec_protos.h>
  15. #include <clib/dos_protos.h>
  16. #include <clib/utility_protos.h>
  17.  
  18. #if defined(__SASC)
  19. #define _USEOLDEXEC_
  20. #include <proto/exec.h>
  21. #include <proto/dos.h>
  22. #include <proto/utility.h>
  23. #endif
  24.  
  25. #include <stdio.h>
  26.  
  27. #include "FSCode.h"
  28. #include "FSCode_rev.h"
  29.  
  30. /***** Libraries *****/
  31. struct DosLibrary    *DOSBase;
  32. struct Library        *UtilityBase;
  33.  
  34. /***** Restituisce la lunghezza di un file giα aperto *****/
  35. static LONG FileSizeFH(BPTR fh)
  36. {
  37.     struct FileInfoBlock *fib;
  38.     LONG size = -1;
  39.  
  40.     if(fib = AllocDosObject(DOS_FIB, NULL))
  41.     {
  42.         if(ExamineFH(fh, fib))
  43.             size = fib->fib_Size;
  44.         FreeDosObject(DOS_FIB, fib);
  45.     }
  46.  
  47.     return size;
  48. }
  49.  
  50. /***** Strings *****/
  51. TEXT StartFmt[] = "!start %s\n";
  52. TEXT   EndFmt[] = "!end %ld %lx\n";
  53.  
  54. TEXT Template[] = "FILE/A,TO,E=ENCODE/S" VERSTAG " by Flavio Stanchina";
  55. enum ARG_INDEX { ARG_FILE, ARG_TO, ARG_ENC, ARG_COUNT };
  56.  
  57. LONG __saveds FSCode(void)
  58. {
  59.     struct RDArgs *rda;
  60.     LONG args[ARG_COUNT];
  61.  
  62.     BPTR in, out = 0;
  63.     LONG tmp;
  64.     BOOL close_out = FALSE;
  65.  
  66.     /* Open libraries (and make sure we're on OS 2.04 or greater) */
  67.     if((DOSBase = OpenLibrary("dos.library", 37)) == NULL)
  68.         return RETURN_FAIL;
  69.     if((UtilityBase = OpenLibrary("utility.library", 37)) == NULL)
  70.         return RETURN_FAIL;
  71.  
  72.     /* Set up arguments */
  73.     args[ARG_FILE] = (LONG)NULL;
  74.     args[ARG_TO  ] = (LONG)NULL;
  75.     args[ARG_ENC ] = (LONG)FALSE;
  76.  
  77.     if(rda = ReadArgs(Template, args, NULL))
  78.     {
  79.         if((STRPTR)args[ARG_TO])
  80.             if(out = Open((STRPTR)args[ARG_TO], MODE_NEWFILE))
  81.                 close_out = TRUE;
  82.             else
  83.             {
  84.                 PrintFault(IoErr(), (STRPTR)args[ARG_TO]);
  85.                 goto no_out;
  86.             }
  87.  
  88.         if(in = Open((STRPTR)args[ARG_FILE], MODE_OLDFILE))
  89.         {
  90.             if(args[ARG_ENC])
  91.             {
  92.                 ULONG crc;
  93.  
  94.                 if(out == 0) out = Output();
  95.  
  96.                 FPrintf(out, StartFmt, FilePart((STRPTR)args[ARG_FILE]));
  97.                 tmp = FileSizeFH(in);
  98.                 crc = Encode(in, out, tmp);
  99.                 FPrintf(out, EndFmt, tmp, crc);
  100.             } else {
  101.                 TEXT line[256];
  102.                 LONG size1, size2;
  103.                 ULONG crc1, crc2;
  104. get_start:
  105.                 if(FGets(in, line, 256) != NULL)
  106.                 {
  107.                     /* Use utility library string compare */
  108.                     if(Strnicmp(line, StartFmt, 7) != 0)
  109.                         goto get_start;
  110.  
  111.                     /* Remove trailing newlines or other control characters */
  112.                     size1 = 7;
  113.                     while(line[size1] >= 32) size1++;
  114.                     line[size1] = '\0';
  115.  
  116.                     /* Try to open specified file */
  117.                     if(out == 0)
  118.                         if(out = Open(line+7, MODE_NEWFILE))
  119.                             close_out = TRUE;
  120.                         else
  121.                         {
  122.                             PrintFault(IoErr(), line+7);
  123.                             goto the_end;
  124.                         }
  125.  
  126.                     tmp = Decode(in, out, &size1, &crc1);
  127.  
  128.                     if(tmp == ENDSTREAMCH)
  129.                     {
  130.                         PutStr("Unexpected EOF\n");
  131.                         goto the_end;
  132.                     }
  133.  
  134.                     if(tmp == '!')
  135.                     {
  136.                         PutStr("Illegal '!' -- File corrupt\n");
  137.                         goto the_end;
  138.                     }
  139. get_end:
  140.                     if(FGets(in, line, 256) != NULL)
  141.                     {
  142.                         if(Strnicmp(line, EndFmt, 5) != 0)
  143.                             goto get_end;
  144.                         sscanf(line, EndFmt, &size2, &crc2);
  145.  
  146.                         if(size1 != size2)
  147.                             PutStr("Size mismatch\n");
  148.                         if(crc1 != crc2)
  149.                             PutStr("CRC mismatch\n");
  150.                     }
  151.                     else PutStr("No end line\n");
  152.                 }
  153.                 else PutStr("No start line\n");
  154.             }
  155. the_end:
  156.             Close(in);
  157.         }
  158.         else PrintFault(IoErr(), (STRPTR)args[ARG_FILE]);
  159.  
  160.         if(close_out) Close(out);
  161. no_out:
  162.         FreeArgs(rda);
  163.     }
  164.     else PrintFault(IoErr(), NULL);
  165.  
  166.     CloseLibrary(DOSBase);
  167.     CloseLibrary(UtilityBase);
  168.  
  169.     return RETURN_OK;
  170. }
  171.